﻿using Ivory;
using Ivory.Interface;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using DefCore;
using DefCore.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DefCore.Tools;
using Defender.GameTypes;
using Defender.Tools;
using System.Reflection;

namespace Defender
{
    public class MainMenu : MainMenuScreen
    {
        //Fields
        private UserInterface _GUI;
        
        //ctor
        public MainMenu(Game game)
            : base(game)
        {

        }

        //Methdos
        public override void RenderMenu(SpriteBatch batch, GameTime time)
        {
            batch.Begin();

            _GUI.DrawGUI(batch, time);
            batch.End();
        }
        public override void UpdateMenu(GameTime time, InputControl input)
        {
            _GUI.UpdateGUI(input, time);
        }
        
        public override bool LoadAssets()
        {
            PropertyInfo[] PinfoArray = typeof(MapLayouts).GetProperties();

            foreach (PropertyInfo pinfo in PinfoArray)
            {
                DefenderLevelFile dlf = new DefenderLevelFile();
                dlf.WorldMapMatrix = (int[,])pinfo.GetValue(null, null);
                dlf.Save(pinfo.Name);
            }


            _GUI = new UserInterface();            

            PropertyInfo[] pinfoArray = typeof(MapLayouts).GetProperties();
            int y = 25;
            foreach (PropertyInfo pinfo in pinfoArray)
            {
                TextButton tpWithMap = new TextButton(FontBin.Instance.GetFnt("Arial12"), string.Format("Map: {0}", pinfo.Name), new Vector2(0, y += 30));
                tpWithMap.ID = string.Format("tpWithMap_{0}", y);
                tpWithMap.Tag = pinfo.Name;

                tpWithMap.LeftClicked += (s, e) =>
                {
                    TestingPit.LevelFileName = (s as TextButton).Tag.ToString();
                    Objects.LevelInstance = Manager.GetScreen(TestingPit.Key) as LevelBase;
                    Manager.SwitchToGameScreen(TestingPit.Key);
                };

                _GUI.AddComponent(tpWithMap);
            }

            return true;
        }
    }
}
